﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace StringMatch
{
    class MatchTester
    {
        public static string textFile = "100k character string.txt";
        public static string p1File = "p1Array.txt";
        public static string p2File = "p2Array.txt";
        public static string p3File = "p3Array.txt";
        public delegate List<int> Matcher(string pattern, string text);
        public string _text;
        //test strings to search for in the text
        public const string _p1 = "cad";
        public const string _p2 = "cccd";
        public const string _p3 = "bababbb";
        List<int> p1Correct;
        List<int> p2Correct;
        List<int> p3Correct;

        /// <summary>
        /// Creates a testing platform for future string matching algorithms. Searches for instances of the pattern in the text
        /// </summary>
        /// <param name="matchAlgorithm"></param>
        /// <param name="pattern"></param>
        /// <param name="text"></param>
        public MatchTester()
        {

            this.p1Correct = new List<int>(); 
            this.p2Correct = new List<int>();
            this.p3Correct = new List<int>();

            //Grab the string to match
            using (StreamReader infile = File.OpenText(textFile))
            {
                this._text = infile.ReadToEnd();
            }
            //grab the correct answers
            using (StreamReader infile = File.OpenText(MatchTester.p1File))
            {
                string line = infile.ReadLine();
                //while the end of the file has not been reached
                while (line != null)
                {
                    //add the lines to the list
                    this.p1Correct.Add(Int32.Parse(line));
                    line = infile.ReadLine();
                }
            }

            using (StreamReader infile = File.OpenText(MatchTester.p2File))
            {
                string line = infile.ReadLine();
                //while the end of the file has not been reached
                while (line != null)
                {
                    //add the lines to the list
                    this.p2Correct.Add(Int32.Parse(line));
                    line = infile.ReadLine();
                }
            }
            
            using (StreamReader infile = File.OpenText(MatchTester.p3File))
            {
                string line = infile.ReadLine();
                //while the end of the file has not been reached
                while (line != null)
                {
                    //add the lines to the list
                    this.p3Correct.Add(Int32.Parse(line));
                    line = infile.ReadLine();
                }
            }

        }

        public bool TestAlgorithm(Matcher matchAlgorithm)
        {
            List<int> p1Result = matchAlgorithm(_p1, this._text);
            List<int> p2Result = matchAlgorithm(_p2, this._text);
            List<int> p3Result = matchAlgorithm(_p3, this._text);

            //Check pattern 1
            if (p1Result.Count() != p1Correct.Count())
                return false;
            for (int i = 0; i < p1Result.Count(); i++)
                if (p1Result[i] != p1Correct[i])
                    return false;

            //Check pattern 2
            if (p2Result.Count != p2Correct.Count())
                return false;
            for (int i = 0; i < p2Result.Count(); i++)
                if (p2Result[i] != p2Correct[i])
                    return false;

            //Check pattern 3
            if (p3Result.Count != p3Correct.Count())
                return false;
            for (int i = 0; i < p3Result.Count(); i++)
                if (p3Result[i] != p3Correct[i])
                    return false;

            return true;
        }
    }
}
